package action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiClass;
import com.intellij.util.ThrowableRunnable;
import model.SPField;
import utils.CodeGenerator;
import utils.Util;
import utils.ZUtils;
import views.SharedPrefsDialog;

import java.util.List;
public class SharedPrefsAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {

        SharedPrefsDialog dialog = new SharedPrefsDialog(e);
        dialog.setOnClickListener(new SharedPrefsDialog.onClickListener() {
            @Override
            public void onOK(List<SPField> SPFieldList) {
                //测试弹窗
                Messages.showInfoMessage(SPFieldList.toString(), "data");

                if (SPFieldList.isEmpty())
                    return;

                FileEditorManager manager = FileEditorManager.getInstance(e.getProject());
                final Editor editor = manager.getSelectedTextEditor();

                final Document document = editor.getDocument();

                Messages.showInfoMessage(e.getProject()+"-"+editor+"-"+document, "data2");


                try {
                    WriteCommandAction.writeCommandAction(e.getProject()).run(new ThrowableRunnable<Throwable>() {
                        @Override
                        public void run() throws Throwable {
                            ZUtils.saveLog("1111111");

//                            document.insertString(0, CodeGenerator.generate(SPFieldList));//可以插入代码

                            PsiClass psiClassFromContext = Util.getPsiClassFromContext(e);

                            int textOffset = psiClassFromContext.getLastChild().getTextOffset();

                            if (!Util.isInitMethodExists(psiClassFromContext)) {
                                String code = CodeGenerator.generateInitMethod();
                                code += "\n \n" + CodeGenerator.generate(SPFieldList);
                                document.insertString(textOffset, code);

                                Messages.showInfoMessage(code+"-"+textOffset+"-"+psiClassFromContext, "data3");

                            } else {
                                document.insertString(textOffset, CodeGenerator.generate(SPFieldList));
                                Messages.showInfoMessage(CodeGenerator.generate(SPFieldList)+"-"+textOffset+"-"+psiClassFromContext, "data4");

                            }
                        }
                    });
                } catch (Throwable throwable) {
                    Messages.showInfoMessage(throwable.toString()+throwable.getMessage()+throwable.getStackTrace().toString(), "data5");

                    ZUtils.saveExceptionLog(throwable);
                    throwable.printStackTrace();
                }
                /*WriteCommandAction.runWriteCommandAction(e.getProject(), new Runnable() {
                    @Override
                    public void run() {
                        PsiClass psiClassFromContext = Util.getPsiClassFromContext(e);


                        int textOffset = psiClassFromContext.getLastChild().getTextOffset();


                        if (!Util.isInitMethodExists(psiClassFromContext)) {
                            String code = CodeGenerator.generateInitMethod();
                            code += "\n \n" + CodeGenerator.generate(SPFieldList);
                            document.insertString(textOffset, code);

                            Messages.showInfoMessage(code+"-"+textOffset+"-"+psiClassFromContext, "data3");

                        } else {
                            document.insertString(textOffset, CodeGenerator.generate(SPFieldList));
                            Messages.showInfoMessage(textOffset+"-"+textOffset+"-"+psiClassFromContext, "data4");

                        }
                    }
                });*/
            }


        });

        dialog.pack();
        //show dialog
        dialog.setVisible(true);

    }

}
